package com.chixing.controller;

import com.chixing.pojo.Housewon;
import com.chixing.service.HousewonService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;


/**
 * @author dwe
 * @date 2021-10-14 9:03
 */
@Controller
public class HousewonController {
    @Autowired
    private HousewonService housewonService;

    //添加新房东房源
    @PostMapping("housewon")
    public ModelAndView save(Housewon housewon){

        ModelAndView mav = new ModelAndView();
        //调用service层，保存Housewon（房东新房源）对象
        ServerResponse serviceResponse = housewonService.saveHouse(housewon);

        if (serviceResponse.getCode() == 200){//添加成功
            //2.查询刚刚添加的房源详情
            ServerResponse serviceResponse1 = housewonService.getByHouseIdAndHousewonName(housewon.getHousewonId(),housewon.getHousewonName());
            //3.绑定数据到页面中，页面跳转
            mav.addObject("serviceResponse",serviceResponse);
            mav.setViewName("house_owner/detail");
        }else {//添加失败，跳转到添加页面
            mav.addObject("addMessage","添加失败");
            mav.setViewName("house_owner/save");
        }
        return null;
    }
}
